package com.gy.zpchat.ui.chat.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import im.zego.zim.callback.ZIMMessageReactionUserListQueriedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMMessage;
import im.zego.zim.entity.ZIMMessageReactionUserInfo;
import im.zego.zim.entity.ZIMMessageReactionUserQueryConfig;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;

/**
 * <p>A fragment that shows a list of items as a modal bottom sheet.</p>
 * <p>You can show this modal bottom sheet from your activity like this:</p>
 * <pre>
 *     ItemListDialogFragment.newInstance(30).show(getSupportFragmentManager(), "dialog");
 * </pre>
 */
public class ItemListDialogFragment extends BottomSheetDialogFragment {

    private static final String ARG_REACTION_TYPE = "reactionType";
    private ItemAdapter adapter;
    private ZIMMessage currentMessage;

    private String mReactionType;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_item_list_dialog_list_dialog, container, false);

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        final RecyclerView recyclerView = (RecyclerView) view;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ItemAdapter();
        recyclerView.setAdapter(adapter);

    }

    public void setArg(String reactionType, ZIMMessage message){
        currentMessage = message;
        this.mReactionType = reactionType;
    }

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        super.show(manager, tag);
        queryReactionUserList(0);
    }

    private void queryReactionUserList(long nextFlag){
        if (currentMessage ==null || TextUtils.isEmpty(mReactionType)){
            return;
        }
        ZIMMessageReactionUserQueryConfig config = new ZIMMessageReactionUserQueryConfig();
        config.reactionType = mReactionType;
        config.count = 100;
        config.nextFlag = nextFlag;
        SDKManager.sharedInstance().queryMessageReactionUserList(currentMessage, config, new ZIMMessageReactionUserListQueriedCallback() {
            @Override
            public void onMessageReactionUserListQueried(ZIMMessage message, ArrayList<ZIMMessageReactionUserInfo> userList, String reactionType, long nextFlag, int totalCount, ZIMError error) {
                if (error.code == ZIMErrorCode.SUCCESS){
                    ArrayList<String> userInfos = new ArrayList<>();
                    for (ZIMMessageReactionUserInfo userInfo : userList) {
                        userInfos.add("userID:"+userInfo.userID);
                    }
                    adapter.addUserList(new ArrayList<>(userInfos));

                    if (nextFlag !=0){
                        queryReactionUserList(nextFlag);
                    }
                }
            }

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    private static class ViewHolder extends RecyclerView.ViewHolder {

        final TextView text;

        ViewHolder(View view) {
            super(view);
            text = view.findViewById(R.id.text);
        }

    }

    private static class ItemAdapter extends RecyclerView.Adapter<ViewHolder> {

        private final ArrayList<String> mUserList = new ArrayList<>();

        public void addUserList (ArrayList<String> userList){
            int start = Math.max(mUserList.size()-1,0);
            mUserList.addAll(userList);
            notifyItemRangeInserted(start,userList.size());
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_item_list_dialog_list_dialog_item, parent, false);
            return new ViewHolder(view);

        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.text.setText(mUserList.get(position));
        }

        @Override
        public int getItemCount() {
            return mUserList.size();
        }

    }
}